package com.bgds.sys.module.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.api.bean.mars.vo.WaveDataVO;
import com.bgds.sys.api.bean.mos.dto.MeasurementDto;
import com.bgds.sys.api.bean.mos.dto.MosDeviceDto;
import com.bgds.sys.api.service.RongZhiService;
import com.bgds.sys.common.constant.BizConstant;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.pagevo.SeriesVo;
import com.bgds.sys.common.utils.ArithUtils;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;

import com.bgds.sys.common.utils.TreeUtils;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.DeviceDynamicAttr;

import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.entity.dto.DeviceDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lj
 */
@Slf4j
@RestController
@Api(tags = "助燃风机监测接口")
@RequestMapping("/combSupportFan")
public class CombustionSupportingFanController {

    @Autowired
    private IDeviceDynamicAttrService iDeviceDynamicAttrService;

    @Autowired
    private InfluxService influxService;

    @Autowired
    private IAlarmLogService iAlarmLogService;
    @Autowired
    private RongZhiService rongZhiService;



    @RequestMapping(value = "/temperature", method = RequestMethod.GET)
    @ApiOperation(value = "获取助燃风机温度监测数据",notes = "获取助燃风机温度监测数据",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", required = true, value = "设备编号(code)"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间"),
    })
    public Result<Map<String,Object>> getDedustFanTemperature(String deviceCode, Long startTime, Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"temperature");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,deviceDynamicAttrs);
        map.put("data",chartData);
        map.put("deviceDynamicAttrs",deviceDynamicAttrs);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }
    @RequestMapping(value = "/trend", method = RequestMethod.GET)
    @ApiOperation(value = "助燃风机趋势分析数据",notes = "助燃风机趋势分析数据",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", required = true,value = "设备编码"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间"),
    })
    public Result<Map<String,Object>> getDeviceChartData(String deviceCode, Long startTime,Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"trend");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,deviceDynamicAttrs);
        map.put("data",chartData);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }

    @RequestMapping(value = "/vibrate/points/{relationId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取助燃风机振动数据监测点",notes = "获取助燃风机振动数据监测点",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "relationId", dataType = "String", required = true,value = "第三设备关联ID"),

    })
    public Result<List<MosDeviceDto>> getPointsByRelationId(@PathVariable("relationId") String relationId){

        List<MosDeviceDto> assetsDtos=  rongZhiService.getAllAssets();
        List<MosDeviceDto> points=assetsDtos.stream().filter(ad->ad.getPid().equals(relationId)).collect(Collectors.toList());
        if(points!=null && points.size()>0) {
            for (MosDeviceDto mosDeviceDto : points) {
                List<MosDeviceDto> childs = assetsDtos.stream().filter(ad -> ad.getPid().equals(mosDeviceDto.getId())).collect(Collectors.toList());
                mosDeviceDto.getMeasurementPoints().addAll(childs);
            }
        }else{
            points=new ArrayList<>();
        }
        return new ResultUtil<List<MosDeviceDto>>().setData(points);
    }
    @RequestMapping(value = "/getMeasurementByPeriod", method = RequestMethod.GET)
    @ApiOperation(value = "获取设备时间区间内的振动读数 开始时间和结束时间距离不能超过1天 最大限制2000条",notes = "获取设备时间区间内的振动读数 开始时间和结束时间距离不能超过1天 最大限制2000条",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "relationId", dataType = "String", required = true, value = "监测点ID"),
            @ApiImplicitParam(name = "startTime", dataType = "String", required = true, value = "起始时间"),
            @ApiImplicitParam(name = "endTime", dataType = "String", required = true, value = "结束时间"),
    })
    public Result<MeasurementDto> getMeasurementByPeriod(String relationId, String startTime, String endTime){
        MeasurementDto measurementDto=  rongZhiService.getMeasurementByPeriod(relationId,startTime,endTime);
        return new ResultUtil<MeasurementDto>().setData(measurementDto);
    }
    @RequestMapping(value = "/getSpectrumByWaveKey", method = RequestMethod.GET)
    @ApiOperation(value = "获取频谱",notes = "获取频谱",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waveKey", dataType = "String", required = true, value = "波形数据key"),
    })
    public Result<List<WaveDataVO> > getSpectrumByWaveKey(String waveKey){
        List<WaveDataVO>  waveDataVos =  rongZhiService.getSpectrumByWaveKey(waveKey);
        return new ResultUtil<List<WaveDataVO>>().setData(waveDataVos);
    }

    @RequestMapping(value = "/getCepstrumByWaveKey", method = RequestMethod.GET)
    @ApiOperation(value = "获取倒谱",notes = "获取倒谱",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waveKey", dataType = "String", required = true, value = "波形数据key"),
    })
    public Result< List<WaveDataVO>> getCepstrumByWaveKey(String waveKey){
        List<WaveDataVO>  waveDataVos=  rongZhiService.getCepstrumByWaveKey(waveKey);
        return new ResultUtil< List<WaveDataVO>>().setData(waveDataVos);
    }
    @RequestMapping(value = "/getVibWaveByWaveKey", method = RequestMethod.GET)
    @ApiOperation(value = "获取振动波形",notes = "获取振动波形",httpMethod = "GET")

    public Result<List<WaveDataVO>> getVibWaveByWaveKey(String waveKey){
        List<WaveDataVO> waveDataVos=  rongZhiService.getVibWaveByWaveKey(waveKey);
        return new ResultUtil<List<WaveDataVO>>().setData(waveDataVos);
    }

    @RequestMapping(value = "/getEnvolopeByWaveKey", method = RequestMethod.GET)
    @ApiOperation(value = "获取包络谱",notes = "获取包络谱",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waveKey", dataType = "String", required = true, value = "波形数据key"),
            @ApiImplicitParam(name = "centerRate", dataType = "Integer", required = true, value = "中心频率"),
            @ApiImplicitParam(name = "bindWidth", dataType = "Integer", required = true, value = "带宽"),
            @ApiImplicitParam(name = "widthRate", dataType = "Integer", required = true, value = "带宽倍数"),
    })
    public Result<List<WaveDataVO>> getEnvolopeByWaveKey(String waveKey, Integer centerRate, Integer bindWidth, Integer widthRate){
        List<WaveDataVO> waveDataVos=  rongZhiService.getEnvolopeByWaveKey(waveKey,centerRate,bindWidth,widthRate);
        return new ResultUtil<List<WaveDataVO>>().setData(waveDataVos);
    }

}
